// components/setText/index.js
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            default: false
        },
        isInfo: {
            type: Boolean,
            default: false
        },
        addressObj: {
            type: Object,
            default: {}
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        newAddressObj: {
            name: '',
            address: '',
            info: '',
            lat: '',
            lon: ''
        }
    },
    observers: {
        'show': function(newVal){
            this.setData({
                newAddressObj: this.data.addressObj || {}
            })
        },
        
    },
    /**
     * 组件的方法列表
     */
    methods: {
        //关闭
        close: function() {
            this.triggerEvent('confirm');  
        },
        
        //选择地址
        selectAddress: function() {
            let _this = this;
            wx.chooseLocation({
                type: 'wgs84',
                success: function (res) {
                    if(res.errMsg == 'chooseLocation:ok'){
                        _this.setData({
                            'newAddressObj.name': res.name,
                            'newAddressObj.address': res.address,
                            'newAddressObj.lat': res.latitude,
                            'newAddressObj.lon': res.longitude
                        })
                    }else{
                        wx.showToast({
                            title: '操作失败，请稍后再试！',
                            icon: 'none'
                        })
                    }
                }
            });
        },
        //门牌号
        setInfo: function(e) {
            this.setData({
                'newAddressObj.info': e.detail || ''
            })
        },
        //保存
        saveAddress: function() {
            let {newAddressObj} = this.data;
            if(!newAddressObj.name || !newAddressObj.lat || !newAddressObj.lon){
                wx.showToast({
                    title: '请设置地址',
                    icon: 'none'
                })
                return;
            }
            this.triggerEvent('confirm',{change: true, addressObj: newAddressObj});  
        },
    }
})









